﻿namespace JNPF.Utils
{
    /// <summary>
    /// 操作者信息
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// </summary>
    public class UserProvider
    {
        /// <summary>
        /// 静态实例
        /// </summary>
        public static UserProvider Instance
        {
            get { return new UserProvider(); }
        }
        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="token">认证信息</param>
        /// <returns></returns>
        public UserInfo Get(string token = null)
        {
            var _token = token == null ? RequestAuthorize.GetRequestToken : token;
            if (_token != null)
                return CacheFactory.Cache.Get<UserInfo>(_token);
            else
                return null;
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="userInfo"></param>
        public void Add(UserInfo userInfo)
        {
            userInfo.Id = CacheKey.LOGINTOKEN + CommonHelper.Guid.ToString();
            //记录会话
            CacheFactory.Cache.Insert(userInfo.Id, userInfo, userInfo.OverdueTime);
            //记录在线
            if (Net.isMobileBrowser == true)
            {
                CacheFactory.Cache.Insert(CacheKey.MOBILELOGINONLINE + userInfo.UserId, userInfo.Id, userInfo.OverdueTime);
                #region 记录移动设备CID,用于消息推送
                if (HttpContext.Current.Request.Headers["clientId"].Count > 0)
                {
                    var clientId = HttpContext.Current.Request.Headers["clientId"].ToString();
                    CacheFactory.Cache.InsertHash(CacheKey.MOBILEDEVICELIST, userInfo.UserId, clientId);
                }
                #endregion
            }
            else
            {
                CacheFactory.Cache.Insert(CacheKey.LOGINONLINE + userInfo.UserId, userInfo.Id, userInfo.OverdueTime);
            }
        }
        /// <summary>
        /// 移除
        /// </summary>
        public void Remove()
        {
            var userInfo = this.Get();
            CacheFactory.Cache.Remove(Net.isMobileBrowser == true ? CacheKey.MOBILELOGINONLINE : CacheKey.LOGINONLINE + userInfo.UserId);
            CacheFactory.Cache.Remove(CacheKey.USERAUTHORIZE + userInfo.UserId);
            CacheFactory.Cache.Remove(userInfo.Id);
        }
        /// <summary>
        /// 移除在线
        /// </summary>
        /// <param name="userId">用户Id</param>
        public void RemoveOnLine(string userId)
        {
            if (userId == null)
                return;
            var onlineToken = CacheFactory.Cache.Get(CacheKey.LOGINONLINE + userId);
            var mobileOnlineToken = CacheFactory.Cache.Get(CacheKey.MOBILELOGINONLINE + userId);
            if (!string.IsNullOrEmpty(onlineToken))
            {
                CacheFactory.Cache.Remove(CacheKey.LOGINONLINE + userId);
                CacheFactory.Cache.Remove(onlineToken);
            }
            if (!string.IsNullOrEmpty(mobileOnlineToken))
            {
                CacheFactory.Cache.Remove(CacheKey.MOBILELOGINONLINE + userId);
                CacheFactory.Cache.Remove(mobileOnlineToken);
            }
        }
        /// <summary>
        /// 是否在线
        /// </summary>
        /// <returns></returns>
        public bool IsOnLine
        {
            get
            {
                var userInfo = this.Get();
                var onlineToken = CacheFactory.Cache.Get((Net.isMobileBrowser == true ? CacheKey.MOBILELOGINONLINE : CacheKey.LOGINONLINE) + userInfo.UserId);
                return onlineToken == userInfo.Id ? false : true;
            }
        }
        /// <summary>
        /// 是否过期
        /// </summary>
        /// <returns></returns>
        public bool IsOverdue
        {
            get
            {
                return this.Get() == null ? false : true;
            }
        }
    }
}
